package com.chlorine.sandbox;

import java.util.ArrayList;

import android.support.v4.app.Fragment;
import android.content.ContentValues;
//import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

/**
 * Display a list of textual items.
 * 
 * @author Caroline Liu
 * 
 */
public class ItemListFragment extends Fragment {

	/**
	 * Reference to the ListView object in this ItemListFragment that displays
	 * the items.
	 */
	private ListView lv;

	/**
	 * Array holding all our list items for display.
	 */
	private ArrayList<Item> array = new ArrayList<Item>();

	/**
	 * An Android object that takes strings in a Java array/ArrayList and puts
	 * it into a ListView.
	 */
	private CustomArrayAdapter adapter;

	private DatabaseHelper db_helper;
	
	/**
	 * Display this Fragment according to the layout specified in frg_list.xml.
	 */
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		super.onCreateView(inflater, container, savedInstanceState); 
		
		// Inflate the layout for this fragment.
		View v = inflater.inflate(R.layout.frg_list, container, false);
		lv = (ListView) v.findViewById(R.id.list);
		
		// TODO: test items
		Item i1 = new Item("Queen of Reversals", "second-lead syndrome ftw", true);
		Item i2 = new Item("Sword and Flower", "pop rock in goryeo is fantastic", false);
		Item i3 = new Item("Sungkyunkwan Scandal", "omg yeorim and georo, you two are killing me. so much swoon, so little screen time!", true);
		Item i4 = new Item("Answer Me, 1997", "hot kisses", true);
		Item i5 = new Item("Delightful Girl Chunhyang", "uhmforce y u so crazy", false);
		array.add(i1);
		array.add(i2);
		array.add(i3);
		array.add(i4);
		array.add(i5);
		
		// Read what's already stored in the db and put it in array so that
		// an adapter can easily display the list items later.
		db_helper = new DatabaseHelper(getActivity(), null);
		SQLiteDatabase db_read = db_helper.getReadableDatabase();
		// Columns that we want to fetch. Technically don't need id right now,
		// but maybe I'll want it in the future.
		String[] cols = {"id", "title", "desc", "checked"};
		// Get rows. Sort by id.
		Cursor c = db_read.query(db_helper.getTableName(), cols, null, null, 
				null, null, "id", null);
		
		// If cursor isn't null, then for each row, get the string and put it
		// in our array. TODO: For now, not processing "checked".
		if (c != null) {
			while (c.moveToNext()) {
				String checked_str = c.getString(3);
				boolean checked = false;
				if (checked_str.equals("true")) {
					checked = true;
				}
				Item i = new Item(c.getString(1), c.getString(2), checked);
				array.add(i);
			}
		}
		
		// Close the cursor and the readable db since we're done w/ them.
		c.close();
		db_read.close();
				
		// Return this Fragment's view.
		return v;
	}

	/**
	 * After the Activity which this ItemListFragment is in has been created,
	 * map this ItemListFragment's array to its ListView for display.
	 */
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		adapter = new CustomArrayAdapter(getActivity(), R.layout.list_item, array);
		lv.setAdapter(adapter);
	}

	/**
	 * Add String s to this ItemListFragment's list.
	 * 
	 * @param s
	 */
	public void addItemToList(String s) {
		// Add s to our temporary array.
		Item i = new Item(s, ":(");
		array.add(i);
		
		// Add s to our database.
		// First, get a writable db.
		SQLiteDatabase db_write = db_helper.getWritableDatabase();
		// Second, build up a ContentValues object -- essentially a set of
		// key-value pairs where the key is the column name and the value is
		// what we want to insert into that column.
		ContentValues cv = new ContentValues(); 
		cv.put("id", db_helper.getNewId());
		cv.put("title", s); 
		cv.put("desc", "N/A");
		cv.put("checked", false);
		// Finally, insert the cv into the table!
		db_write.insert(db_helper.getTableName(), null, cv);
		db_write.close();
		
		// Tell the adapter that our array has changed. The adapter will
		// refresh the ListView to display the new data without us saying
		// anything more.
		adapter.notifyDataSetChanged();
	}

}